package jinyilw.swing.visuals;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.rlaf.theme.RColorSet;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.ui.RComboBoxUI;
import jinyilw.swing.rlaf.ui.icon.RIconFactory.RadioButtonIcon;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class CombinedGroupPanel extends JPanel
{
	private static final long serialVersionUID = -5666845557784405041L;
	private final ButtonGroup buttongroup = new ButtonGroup();
	private final ArrayList<JRadioButton> list = new ArrayList<>();
	private JComboBox comboBox;
	private SelectComBoxActionListener selectComBoxActionListener;
	private SelectRadioActionListener selectRadioActionListener;
	private ActionListener thisActionListener;
	private boolean isReset = false;

	public CombinedGroupPanel()
	{

	}

	public void setSelectedIndex(int index)
	{
		if (index > -1 && index < list.size())
		{
			list.get(index).setSelected(true);
			if (isReset && comboBox != null)
				comboBox.setSelectedIndex(0);
		} else if (index >= list.size() && comboBox != null)
		{
			int i = isReset ? index - list.size() + 1 : index - list.size();
			comboBox.setSelectedIndex(i);
		}
	}

	public void add(JRadioButton ab)
	{
		super.add(ab);
		buttongroup.add(ab);
		list.add(ab);
	}
	public void addOther(Component com)
	{
		super.add(com);
	}

	public void setComboBox(JComboBox comboBox, boolean b)
	{
		setComboBox(comboBox);
		isReset = b;
	}

	public void setComboBox(JComboBox comboBox)
	{
		JComboBox oldComboBox = this.comboBox;
		if ((oldComboBox == null && comboBox == null)
				|| (oldComboBox != null && oldComboBox.equals(comboBox)))
			return;
		if (oldComboBox != null)
		{
			remove(oldComboBox);
			oldComboBox.removeActionListener(selectComBoxActionListener);
		}
		if (comboBox != null)
		{
			super.add(comboBox);
			comboBox.setUI(new CBComboBoxUI());
			comboBox.addActionListener(getSelectComBoxActionListener());
			if (oldComboBox == null)
				for (AbstractButton b : list)
					b.addActionListener(getSelectRadioActionListener());
		}
		this.comboBox = comboBox;
	}

	public int getSelectIndex()
	{
		int i;
		for (i = 0; i < list.size(); i++)
			if (list.get(i).isSelected())
				return i;
		// System.out.println("next");
		if (comboBox != null)
		{
			int index = comboBox.getSelectedIndex();
			if (index != -1)
				return isReset ? index + list.size() - 1 : index + list.size();
		}
		return -1;
	}

	public void addActionListener(ActionListener listener)
	{
		for (AbstractButton b : list)
			b.addActionListener(listener);
		thisActionListener = listener;
	}

	private SelectRadioActionListener getSelectRadioActionListener()
	{
		if (selectRadioActionListener == null)
			selectRadioActionListener = new SelectRadioActionListener();
		return selectRadioActionListener;
	}

	private SelectComBoxActionListener getSelectComBoxActionListener()
	{
		if (selectComBoxActionListener == null)
			selectComBoxActionListener = new SelectComBoxActionListener();
		return selectComBoxActionListener;
	}

	private class SelectRadioActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			if (isReset && comboBox != null)
				comboBox.setSelectedIndex(0);
		}
	}

	private class SelectComBoxActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			if (comboBox == null)
				return;
			if (isReset && comboBox.getSelectedIndex() == 0)
				return;
			for (JRadioButton radioButton : list)
				if (radioButton != null)
				{
					buttongroup.remove(radioButton);
					radioButton.setSelected(false);
					buttongroup.add(radioButton);
				}

			if (thisActionListener != null)
				thisActionListener.actionPerformed(e);
		}
	}
}

class CBComboBoxUI extends RComboBoxUI
{

	@Override
	public void update(Graphics g, JComponent c)
	{

		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, c, 0.92f,
				0.3f);
		renderingConfig.setAAPainting(g2d);
		int w = c.getWidth();
		int h = c.getHeight();
		if (comboBox.getSelectedIndex() > 0)
		{
			int oR = RadioButtonIcon.oR, dR = RadioButtonIcon.dR;
			int x = 4, y = (h - oR) / 2;
			float fx = x, fy = y;
			g2d.translate(fx, fy);
			g2d.setPaint(RadioButtonIcon.borderPaint);
			g.fillOval(0, 0, oR, oR);
			g.setColor(RadioButtonIcon.lightTransColor);
			g.fillOval(1, 1, oR - 2, oR - 2);
			x = (oR - dR) / 2;
			y = (oR - dR) / 2;
			g.setColor(RColorSet.blackBGColor);
			g.fillOval(x, y, dR, dR);
			g2d.translate(-fx, -fy);
			padding.left = 12;
		} else
			padding.left = 4;

		if (triangle != null)
		{
			g2d.setColor(RThemeColor.colorSet.textColor);
			int x = w - triangleSize - 4;
			int y = (h - triangleSize) / 2 + 1;
			paintTriangle(g2d, x, y);
		}
		renderingConfig.resetAAPainting(g2d);
		paint(g, c);
		if (composite != null)
			g2d.setComposite(composite);
	}
}
